package com.mxchip.livestar.rest.log


import android.text.TextUtils
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.ObservableTransformer
import io.reactivex.functions.Function
import okhttp3.ResponseBody
import java.io.IOException

object RxResultHelper {
    fun filterResultToString(): ObservableTransformer<ResponseBody, String> {
        return ObservableTransformer { upstream ->
            upstream.flatMap { responseBody ->
                var string = ""
                try {
                    string = responseBody.string()
                } catch (e: IOException) {
                    e.printStackTrace()
                }

                createData(string)
            }
        }
    }

    /**
     * 当messageCode 返回不为0000的时候, 将调用callback.event(BaseEntity<T>)
     */
    fun <T> filterErrorResult(callback: Callback<BaseEntity<T>>): ObservableTransformer<BaseEntity<T>, BaseEntity<T>> {
        return ObservableTransformer { upstream ->
            upstream.flatMap { it ->
                if (!it.isOK()) {
                    callback.event(it)
                }
                Observable.just(it)
            }
        }
    }

    fun <T> handleResult(): ObservableTransformer<BaseEntity<T>, T> = handleResult(false, true, true)

    fun <T> handleResult(isCouldReturnNull: Boolean): ObservableTransformer<BaseEntity<T>, T> = handleResult(isCouldReturnNull, true, true)

    fun <T> handleResult(isCouldReturnNull: Boolean, isNeedToLogin: Boolean): ObservableTransformer<BaseEntity<T>, T> = handleResult(isCouldReturnNull, isNeedToLogin, true)

    fun <T> handleResult(isCouldReturnNull: Boolean, isNeedToLogin: Boolean, isJumpDialog: Boolean): ObservableTransformer<BaseEntity<T>, T> {
        return ObservableTransformer { upstream ->
            upstream.flatMap(Function<BaseEntity<T>, ObservableSource<T>> { entity ->
                var code: String? = "5000"
                var msg: String? = "出错了，请稍后重试"
                if (entity != null && entity.isOK()) {
                    //防止某些接口返回data为null
                    if (entity.data == null) {
                        if (isCouldReturnNull) {
                            entity.data = Object() as T
                        } else {
                            entity.data = java.lang.Boolean.valueOf(true) as T
                        }
                    }

                    return@Function createData(entity.data)
                } else {
                    if (entity != null) {
                        code = entity.code
                        //添加错误消息判定
                        if (!TextUtils.isEmpty(entity.message)) {
                            msg = entity.message
                        } else {
                            msg = entity.result
                            //如果服务器返回的消息为空，则通过错误码判定
                            if (TextUtils.equals(entity.code, "0001")) {
                                msg = entity.result
                            }
                        }


                    }
                }
                var retrunCode: String? = code;
                var retrunMsg: String? = msg;
                if (retrunCode == null) {
                    retrunCode = ""
                }
                if (retrunMsg == null) {
                    retrunMsg = ""
                }
                if (retrunCode == "1048" || retrunCode == "1050" || retrunCode == "1053" || retrunCode == "1057" || retrunCode == "1058"
                        || retrunCode == "1059" || retrunCode == "1060" || retrunCode == "1061" || retrunCode == "4013" || retrunCode == "4014"
                        || retrunCode == "7098") {
                    retrunMsg = ""
                }

                Observable.error(com.mxchip.livestar.rest.log.ApiException(retrunCode, retrunMsg))

//                if (code != null && msg != null)
//                    Observable.error(ApiException(code, msg))
//                else
//                    Observable.error(ApiException("", ""))
            })
        }
    }

    private fun <T> createData(t: T): Observable<T> {
        return Observable.create { emitter ->
            try {
                emitter.onNext(t)
                emitter.onComplete()
            } catch (e1: Exception) {
                e1.printStackTrace()
//                emitter.onError(e1)
            }
        }
    }
}